package rexsee.device;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.telephony.TelephonyManager;

public class RexseeTelephony implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Telephony";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeTelephony(childBrowser);
	}

	private final Context mContext;
	private final Browser mBrowser;
	public RexseeTelephony(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public String getDataActivity() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getDataActivity()) {
			case TelephonyManager.DATA_ACTIVITY_IN :
				return "DATA_ACTIVITY_IN";
			case TelephonyManager.DATA_ACTIVITY_OUT :
				return "DATA_ACTIVITY_OUT";
			case TelephonyManager.DATA_ACTIVITY_INOUT :
				return "DATA_ACTIVITY_INOUT";
			case TelephonyManager.DATA_ACTIVITY_NONE :
				return "DATA_ACTIVITY_NONE";
			case 4 : // TelephonyManager.DATA_ACTIVITY_DORMANT, for SDK 3+
				return "DATA_ACTIVITY_DORMANT";
			default :
				return "DATA_ACTIVITY_NONE";
		}
	}
	public String getDataState() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getDataState()) {
			case TelephonyManager.DATA_DISCONNECTED :
				return "DATA_DISCONNECTED";
			case TelephonyManager.DATA_CONNECTING :
				return "DATA_CONNECTING";
			case TelephonyManager.DATA_CONNECTED :
				return "DATA_CONNECTED";
			case TelephonyManager.DATA_SUSPENDED :
				return "DATA_SUSPENDED";
			default :
				return "DATA_DISCONNECTED";
		}
	}
	public String getDeviceId() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}
	public String getDeviceSoftwareVersion() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceSoftwareVersion();
	}
	public String getLine1Number() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getLine1Number();
	}
	public String getNetworkCountryIso() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getNetworkCountryIso();
	}
	public String getNetworkOperator() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getNetworkOperator();
	}
	public String getNetworkOperatorName() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getNetworkOperatorName();
	}
	public String getNetworkType() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getNetworkType()) {
			case TelephonyManager.NETWORK_TYPE_UNKNOWN :
				return "NETWORK_TYPE_UNKNOWN";
			case TelephonyManager.NETWORK_TYPE_GPRS :
				return "NETWORK_TYPE_GPRS";
			case TelephonyManager.NETWORK_TYPE_EDGE :
				return "NETWORK_TYPE_EDGE";
			case TelephonyManager.NETWORK_TYPE_UMTS :
				return "NETWORK_TYPE_UMTS";
			case 4 : // TelephonyManager.NETWORK_TYPE_CDMA, for SDK 3+
				return "NETWORK_TYPE_CDMA";
			case 5 : // TelephonyManager.NETWORK_TYPE_EVDO_0, for SDK 3+
				return "NETWORK_TYPE_EVDO_0";
			case 6 : // TelephonyManager.NETWORK_TYPE_EVDO_A, for SDK 3+
				return "NETWORK_TYPE_EVDO_A";
			case 7 : // TelephonyManager.NETWORK_TYPE_1xRTT, for SDK 3+
				return "NETWORK_TYPE_1xRTT";
			case 8 : // TelephonyManager.NETWORK_TYPE_HSDPA, for SDK 5+
				return "NETWORK_TYPE_HSDPA";
			case 9 : // TelephonyManager.NETWORK_TYPE_HSUPA, for SDK 5+
				return "NETWORK_TYPE_HSUPA";
			case 10 : // TelephonyManager.NETWORK_TYPE_HSPA, for SDK 5+
				return "NETWORK_TYPE_HSPA";
			default :
				return "NETWORK_TYPE_UNKNOWN";
		}
	}
	public String getPhoneType() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getPhoneType()) {
			case TelephonyManager.PHONE_TYPE_NONE :
				return "PHONE_TYPE_NONE";
			case TelephonyManager.PHONE_TYPE_GSM :
				return "PHONE_TYPE_GSM";
			case 2 : // TelephonyManager.PHONE_TYPE_CDMA, for SDK 3+
				return "PHONE_TYPE_CDMA";
			default :
				return "PHONE_TYPE_NONE";
		}
	}

	public String getSimCountryIso() {
		if (!isSimReady()) return "";
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimCountryIso();
	}
	public String getSimOperator() {
		if (!isSimReady()) return "";
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimOperator();
	}
	public String getSimOperatorName() {
		if (!isSimReady()) return "";
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimOperatorName();
	}
	public String getSimSerialNumber() {
		if (!isSimReady()) return "";
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimSerialNumber();
	}
	public String getSimState() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getSimState()) {
			case TelephonyManager.SIM_STATE_ABSENT :
				return "SIM_STATE_ABSENT";
			case TelephonyManager.SIM_STATE_UNKNOWN :
				return "SIM_STATE_UNKNOWN";
			case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
				return "SIM_STATE_NETWORK_LOCKED"; //Need NetworkPIN
			case TelephonyManager.SIM_STATE_PIN_REQUIRED :
				return "SIM_STATE_PIN_REQUIRED"; //Need PIN
			case TelephonyManager.SIM_STATE_PUK_REQUIRED :
				return "SIM_STATE_PUK_REQUIRED"; //Need PUK
			case TelephonyManager.SIM_STATE_READY :
				return "SIM_STATE_READY";
			default :
				return "SIM_STATE_UNKNOWN";
		}
	}
	public String getSubscriberId() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSubscriberId();
	}
	public String getVoiceMailAlphaTag() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getVoiceMailAlphaTag();
	}
	public String getVoiceMailNumber() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getVoiceMailNumber();
	}
	public boolean isNetworkRoaming() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.isNetworkRoaming();
	}
	public boolean isSimReady() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return (tm.getSimState() == TelephonyManager.SIM_STATE_READY) ? true : false;
	}

}
